﻿using System;
using System.Linq;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace HeatMapPrototype
{
    /// <summary>
    /// Summary description for InsertCoordinates
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    //[System.Web.Script.Services.ScriptService]
    public class InsertCoordinates : System.Web.Services.WebService
    {    
        [WebMethod]

        public void Insert(string xCoordinates, string yCoordinates)
        {
            string[] xCoords = xCoordinates.Split(',');
            string[] yCoords = yCoordinates.Split(',');

            //The following is a ZIP operation which allows you to do a foreach loop on two arrays.
            var xANDyCoordinates = xCoords.Zip(yCoords, (x, y) => new { xCoord = x, yCoord = y });
            
            foreach (var xy in xANDyCoordinates)
            {
                SqlConnection connection = new SqlConnection(GetConnectionString());
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd = new SqlCommand("InsertIndividualHeatMapCoordinates", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("xCoord", xy.xCoord));
                cmd.Parameters.Add(new SqlParameter("yCoord", xy.yCoord));                
                cmd.ExecuteNonQuery();
            }            
        }

        private string GetConnectionString()
        {
            return ConfigurationManager.AppSettings["SQLConn"];
        }
    }
}
